package ui

import applicationModel.AlmanaquePlantas
import applicationModel.Ataque
import applicationModel.BuscadorZombie
import applicationModel.Fila
import applicationModel.Plantar
import dominio.JardinTablero
import dominio.Jugador
import java.util.List
import org.uqbar.arena.bindings.NotNullObservable
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.layout.HorizontalLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Label
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.widgets.tables.Column
import org.uqbar.arena.widgets.tables.Table
import org.uqbar.arena.windows.Dialog
import org.uqbar.arena.windows.WindowOwner
import org.uqbar.commons.utils.Observable
import org.uqbar.arena.bindings.ObservableProperty

@Observable
class JardinTableroWindow extends Dialog<JardinTablero> {

	//@Property Jugador jug
	@Property List<Fila> results

	new(WindowOwner owner, JardinTablero jt) {
		super(owner, jt)
		//modelObject.setJugador(jugador)
		title = "Plants Vs Zombies Game"
		taskDescription = "comienza el juego!!!"
		//jug = jugador
		modelObject.start
		results = modelObject.getFilas
	}

	override protected addActions(Panel actionsPanel) {

		val botonZen = new Button(actionsPanel).setAsDefault.setCaption("Jardin Zen")
		botonZen.onClick[|this.jardinAcuatico]

		val botonAlmanaqueZombies = new Button(actionsPanel).setAsDefault.setCaption("Almanaque Zombie")
		botonAlmanaqueZombies.onClick[|this.almanaqueZombie]

		val botonAlmanaquePlantas = new Button(actionsPanel).setAsDefault.setCaption("Almanaque Plantas")
		botonAlmanaquePlantas.onClick[|this.almanaquePlantas]

	}

	def atacar() {
		this.openDialog(new AtacarWindow(this, new Ataque(modelObject.filaSeleccionada,modelObject.jugador), modelObject))
	}

	def plantar() {
		this.openDialog(new PlantarWindow(this, new Plantar(modelObject.filaSeleccionada), modelObject,modelObject.jugador))
	}

	def openDialog(Dialog<?> dialog) {
		dialog.onAccept[|]
		dialog.open

	}

	def jardinAcuatico() {
		this.openDialog(new JardinAcuaticoWindow(this, modelObject.jugador))
	}

	def almanaqueZombie() {
		this.openDialog(new AlmanaqueZombieWindow(this, new BuscadorZombie))
	}

	def almanaquePlantas() {
		this.openDialog(new AlmanaquePlantasWindow(this, new AlmanaquePlantas))
	}

	override protected createFormPanel(Panel mainPanel) {
		val editorPanel = new Panel(mainPanel)
		editorPanel.setLayout(new ColumnLayout(80))

		new Label(editorPanel).setText("Nombre Jugador: ")

		new Label(editorPanel).setText(modelObject.jugador.nombre)

		new Label(editorPanel).setText("Dinero: ")
		new Label(editorPanel).bindValue(new ObservableProperty(this.modelObject,"jugador.dinero"))
		
		this.createResultsGrid(mainPanel)
		this.createGridActions(mainPanel)
	}

	def void createResultsGrid(Panel mainPanel) {
		var table = new Table<Fila>(mainPanel, typeof(Fila))
		table.heigth = 200
		table.width = 450
		table.bindItemsToProperty("filas")
		table.bindValueToProperty("filaSeleccionada")
		this.describeResultsGrid(table)
	}

	def void describeResultsGrid(Table<Fila> table) {

		new Column<Fila>(table) 
		.setTitle("Terreno").setFixedSize(100).bindContentsToProperty("tipo")

		new Column<Fila>(table) 
		.setTitle("Plantas plantadas en Fila").setFixedSize(1000).bindContentsToProperty("plantas")

	}

	def createGridActions(Panel mainPanel) {
		var actionsPanel = new Panel(mainPanel)
		actionsPanel.setLayout(new HorizontalLayout)
		var edit = new Button(actionsPanel).setCaption("Atacar").onClick[|this.atacar()]

		var remove = new Button(actionsPanel).setCaption("Plantar").onClick[|this.plantar()]

		var elementSelected = new NotNullObservable("filaSeleccionada")
		remove.bindEnabled(elementSelected)
		edit.bindEnabled(elementSelected)
	}

}
